package test.niuke;

import java.util.ArrayList;

/**
 * 滑动窗口求窗口中的最大值
 */
public class MaxInWindow {
    public ArrayList<Integer> maxInWindows(int[] num, int size) {
        ArrayList<Integer> retList = new ArrayList();
        if (size == 0 || num.length < size) {
            return retList;
        }
        int max = getMax(num, 0, size - 1);
        retList.add(max);
        int newIndexTail = size;
        while (newIndexTail <= num.length - 1) {
            if (num[newIndexTail] > max) {
                max = num[newIndexTail];
            } else if (num[newIndexTail - size] == max) {
                max = getMax(num, newIndexTail - size + 1, newIndexTail);
            }
            retList.add(max);
            newIndexTail++;
        }
        return retList;
    }

    private int getMax(int[] array, int start, int end) {
        int max = array[start];
        for (int i = start; i <= end; i++) {
            if (array[i] > max) {
                max = array[i];
            }
        }
        return max;
    }

    public static void main(String[] args) {
        MaxInWindow maxInWindow = new MaxInWindow();
        System.out.println(maxInWindow.maxInWindows(new int[]{16, 14, 12, 10, 8, 6, 4}, 5));
    }
}
